/**
 * 郭楠project
 * com.gn.controller
 * GoodsController.java
 * 创建人:TonyGuo 
 * 时间：2017年3月23日-下午2:39:48 
 * 2017Tm公司-版权所有
 */
package com.gn.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gn.entity.Goods;
import com.gn.entity.R;
import com.gn.service.CommentService;
import com.gn.service.GoodsService;

/**
 * 
 * GoodsController
 * 创建人:TonyGuo
 * 时间：2017年3月23日-下午2:39:48 
 * @version 1.0.0
 * 
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
	Logger logger = LoggerFactory.getLogger(GoodsController.class);
	@Resource
	GoodsService goodsService;
	@Resource
	CommentService commentService;
	/**
	 * 添加Goods
	 * */
	@RequestMapping("/save")
	public R addGoods(Goods goods){
		goodsService.addGoods(goods);
		return R.ok();
	}
	
	/**
	 * 修改Goods
	 * */
	@RequestMapping("/update")
	public R update(Goods goods){
		goodsService.update(goods);
		return R.ok();
	}
	
	/**
	 * 删除Goods
	 * */
	@RequestMapping("/del/{id}")
	public R delete(@PathVariable("id") String id){
		goodsService.delete(id);
		return R.ok();
	}
	
	/**
	 * 查询Goods
	 * @since  1.0.0
	 */
	@RequestMapping("/get/{id}")
	public R findGoodsById(@PathVariable("id") String id){
		Goods goods=goodsService.findGoodsById(id);
		return R.ok().put("goods", goods);
	}
	/**
	 * 查询列表
	 * */
	@RequestMapping("/all")
	public List<Goods> findAll(@RequestParam(name="s",defaultValue="",required=false) String searchCnt){
		System.out.println("search:"+searchCnt);
		if(!"".equals(searchCnt.trim())){
			searchCnt = "%"+searchCnt+"%";
		}
		List<Goods> list=goodsService.findAll(searchCnt);
		if(list!=null&&list.size()!=0){
			return list;
		}
		return null;
	}
	
	/**
	 * 查询商品总数
	 * */
	@RequestMapping("/count")
	public R findGoodsCount(){
		Integer count=goodsService.findTotalRecords();
		count = count != null ? count : 0;
		Map map=new HashMap<String,Object>();
		map.put("count", count);
		return R.ok(map);
	}
	
	/**
	 * 更新阅读量
	 * */
	@RequestMapping("/readcount.update")
	public R updateReadCount(String id) {
		Goods goods = goodsService.findGoodsById(id);
		logger.info("=====goods:【{}】===",goods.getId());
		String c=goods.getReadCount();
		Integer count=Integer.valueOf(c);
		count+=1;
		goods.setReadCount(""+count);
		goodsService.update(goods);
		return R.ok();
	}
}
